package com.kjtpay.design_pattern.prototype;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.proxy.ProxyActivity;

public class PrototypeActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, PrototypeActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    TextView displayView;
    Resume xiaoCai;
    Resume xiaoCaiCopy;
    Resume daNiao;
    Resume daNiaoCopy1;
    Resume daNiaoCopy2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prototype);
        setTitle("原型模式");

        displayView = findViewById(R.id.display);

        xiaoCai = new Resume("小菜");
        xiaoCai.setPersonalInfo("男", "24");
        xiaoCai.setWorkExperience("2000-2002", "学校软件公司");

        try {
            xiaoCaiCopy = (Resume) xiaoCai.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        try {
            daNiao = (Resume) xiaoCai.clone();
            daNiao.setName("大鸟");
            daNiao.setPersonalInfo("男","28");
            daNiao.setWorkExperience("2004-2005", "XX公司");
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        try {
            daNiaoCopy1 = (Resume) daNiao.clone();
            daNiaoCopy1.setWorkExperience("2006-2008", "YY公司");
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        try {
            daNiaoCopy2 = (Resume) daNiao.clone();
            daNiaoCopy2.setWorkExperience("2010-2011", "ZZ公司");
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    public void onXiaoCaiClick(View view) {
        displayView.setText(xiaoCai.display());
    }

    public void onXiaoCaiCopyClick(View view) {
        displayView.setText(xiaoCaiCopy.display());
    }

    public void onDaNiaoClick(View view) {
        displayView.setText(daNiao.display());
    }

    public void onDaNiaoCopy1Click(View view) {
        displayView.setText(daNiaoCopy1.display());
    }

    public void onDaNiaoCopy2Click(View view) {
        displayView.setText(daNiaoCopy2.display());
    }
}